package com.sun.lib.util;

import android.graphics.Paint;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

/*
 * Created by Administrator on 2019/5/13.
 *
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */
public class TextViewUtil {
    private static TextViewUtil instance = null;

    private TextViewUtil() {
    }

    public static TextViewUtil get() {
        if (null == instance) {
            synchronized (TextViewUtil.class) {
                if (null == instance)
                    instance = new TextViewUtil();
            }
        }
        return instance;
    }

    /**
     * 获取textview宽度
     * @param view
     * @param drawableWidth
     * @param paddingLeft
     * @param paddingRight
     * @param drawablePaddingHorizontal
     * @return
     */
    public float getTextWidth(TextView view, int drawableWidth, int paddingLeft, int paddingRight, int drawablePaddingHorizontal) {
        if (view == null) return 0;
        float textWidth = 0;
        String originalText = view.getText().toString();
        if (originalText.contains("\n")) {
            String[] originalTextArrays = originalText.split("\n");
            int arrayLength = originalTextArrays.length;
            ArrayList<Float> widthList = new ArrayList<>(arrayLength);
            for (int i = 0; i < arrayLength; i++) {
                widthList.add(view.getPaint().measureText(originalTextArrays[i]));
            }

            textWidth = Collections.max(widthList);
        } else {
            textWidth = view.getPaint().measureText(originalText);
        }

        int maxWidth = view.getWidth() - drawableWidth - paddingLeft - paddingRight - drawablePaddingHorizontal;
        if (textWidth > maxWidth) {
            textWidth = maxWidth;
        }

        return textWidth;
    }

    /**
     * 获取textview高度
     * @param view
     * @param drawableHeight
     * @param paddingTop
     * @param paddingBottom
     * @param drawablePaddingVertical
     * @return
     */
    public float getTextHeight(TextView view, int drawableHeight, int paddingTop, int paddingBottom, int drawablePaddingVertical) {
        if (view == null) return 0;
        Paint.FontMetrics fontMetrics = view.getPaint().getFontMetrics();
        float singLineHeight = Math.abs(fontMetrics.bottom - fontMetrics.top);
        float textHeight = singLineHeight * view.getLineCount();
        int maxHeight = view.getHeight() - drawableHeight - paddingTop - paddingBottom - drawablePaddingVertical;
        if (textHeight > maxHeight)
            textHeight = maxHeight;
        return textHeight;
    }
}
